package approve.action;

import java.util.List;

import nguyen.dao.ReceiverDocumentDAO;

import model.pojo.Document;
import model.pojo.Employee;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Re_NotApprovedDocument extends Activity {
	String database = "admin";
	Bundle language=null;
	String lang = "EN";
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			lang=extras.getString("lang");
			language=extras.getBundle(lang);
		}
		return database;
	}
	Employee emp=null;
	
	ListView listView;	


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.re_notapproveddocument);
		getDatabase();
		
	    emp=(Employee) getIntent().getExtras().get("em");

		listView = (ListView) findViewById(R.id.notapproveddocumentlist);
		
		List<Document> listDocs=getNotApprovedList();
		if(language!=null){
        	TextView la_headerTitle=(TextView)findViewById(R.id.la_headerTitle);
        	la_headerTitle.setText(language.getString("not-approved-document")+" ("+listDocs.size()+"):");
        }
		
		
		if(listDocs.size()>0){
			listView.setAdapter(new ArrayAdapter<Document>(this,
					android.R.layout.simple_list_item_1,listDocs ));
			listView.setOnItemClickListener(approveDocument);
		}else{
				TextView empltyList=(TextView) findViewById(R.id.empltyList);
				if(language!=null){
					empltyList.setText(language.getString("no_data"));
			}
		}
		
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	        // do something on back.
			Intent intent=new Intent(Re_NotApprovedDocument.this,MainFrame.class);
			intent.putExtras(getIntent().getExtras());
			startActivityForResult(intent, 0);
			
	        return true;
	    }

		return super.onKeyDown(keyCode, event);
	}
	
	private OnItemClickListener approveDocument=new OnItemClickListener() {

		public void onItemClick(AdapterView<?> listDoc, View arg1, int arg2,
				long arg3) {
			Document doc=(Document) listDoc.getItemAtPosition(arg2);
			
			if(doc.getTypeDoc()<11){
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm_lessthan10.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			else if(doc.getTypeDoc()==12){
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm12.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			else if(doc.getTypeDoc()==13){
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm13.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			else if(doc.getTypeDoc()==15){
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm15.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			else if(doc.getTypeDoc()==16){
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm16.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			else{
				Intent intent=new Intent(Re_NotApprovedDocument.this,Re_ApproveForm14.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivityForResult(intent, 0);
			}
			
		}
		
	};
	
	public List<Document> getNotApprovedList() {
		return ReceiverDocumentDAO.getNotApprovedDocument(emp.getIdemployee(), database,lang);
	}

//	public List<Document> getNotApprovedList() {
//		String sql = "SELECT approvedoc.* FROM approvedoc,listapprove WHERE IDTypeDoc != 11 and listapprove.IDEmployee='1322887525530'"
//					+" AND listapprove.IDGroupApprove = approvedoc.IDGroupApprove AND listapprove.IDStatus=2 " +
//						" AND approvedoc.IDStatus=2";
//		String host = "14.63.219.99";
//		String database = "elihan";
//		String url = "jdbc:mysql://" + host + ":3306/" + database;
//		String user = "erp";
//		String password = "nguyen";
//		Connection con = null;
//
//		List<Document> list = new ArrayList<Document>();
//		try {
//			DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
//			con = (Connection) DriverManager.getConnection(url, user, password);
//			Statement statement = (Statement) con.createStatement();
//			ResultSet rs = statement.executeQuery(sql);
//			Document doc=null;
//			while(rs.next()){	
//				doc=new Document(rs.getInt("IDApproveDoc"),rs.getString("Tile"),rs.getString("Description"),rs.getString("Content"));
//				list.add(doc);
//		
//			}
//		} catch (SQLException e) {
//			openDialog();
//			System.out.println("ERROR CONNECT");
//			e.printStackTrace();
//		} finally {
//			try {
//				con.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		}
//		return list;
//
//	}

	public void openDialog() {
		// TODO Auto-generated method stub

	}

}
